package psn.hui.sys.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.Menu;
import psn.hui.sys.domain.Role;
import psn.hui.sys.service.MenuService;
import psn.hui.sys.service.RoleService;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;
import psn.hui.sys.utils.TreeNode;
import psn.hui.sys.vo.RoleVo;

/**
 * 	角色管理控制器
 * 
 * @author hui
 *
 */
@RestController
@RequestMapping("role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * 	获取角色列表，分页，查询
	 */
	@RequestMapping("roleList")
	public DataGridView roleList(RoleVo roleVo) {
		List<Role> rolelist = null;
		Page<Role> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
		rolelist = roleService.queryAllRoleForList(roleVo);
		return  new DataGridView(page.getTotal(), rolelist);
	}
	/**
	 * 	添加角色
	 * @param menu
	 * @return
	 */
	@RequestMapping("addRole")
	public JsonResult addRole(Role role) {
		try {
			int rs = roleService.insertSelective(role);
			if(rs > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	/**
	 * 	修改角色
	 * @param menu
	 * @return
	 */
	@RequestMapping("updateRole")
	public JsonResult updateRole(Role role) {
		try {
			int rs = roleService.updateByPrimaryKeySelective(role);
			if(rs > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	/**
	 * 	根据id删除角色
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteById")
	public JsonResult deleteById(Integer roleid) {
		try {
			int rs = roleService.deleteById(roleid);
			if(rs > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	根据角色id批量删除角色数据，有关联删除
	 * @param roleVo
	 * @return
	 */
	@RequestMapping("batchDeleteRole")
	public JsonResult batchDeleteRole(RoleVo roleVo) {
		try {
			int rs = roleService.batchDeleteRoleByIds(roleVo.getIds());
			if(rs > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	获取全部菜单列表，且根据角色id查询其拥有的菜单，将相应菜单标记为选中
	 * @param roleid
	 * @return
	 */
	@RequestMapping("initMenuTreeByRid")
	public DataGridView initMenuTreeByRid(int roleid) {
		//先查询所有的菜单列表
		Menu menu = new Menu();
		menu.setAvailable(SysConstant.MENU_AVAILABLE_TRUE);
		List<Menu> menuList = menuService.queryAllMenuForList(menu);
		//根据角色id查询其拥有的菜单 id
		List<Menu> menuList2 = menuService.queryMenuByRid(roleid);
		
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		//循环遍历所有菜单
		for (Menu m1 : menuList) {
			//标记是否选中 0不选中 1选中
			String checkArr = "0";
			for(Menu m2 : menuList2) {
				if(m1.getId() == m2.getId()) {
					checkArr = "1";
					break;
				}
			}
			Boolean spread = m1.getSpread()==SysConstant.MENU_SPREAD_TRUE;
			TreeNode node = new TreeNode();
			node.setId(m1.getId());
			node.setPid(m1.getPid());
			node.setTitle(m1.getTitle());
			node.setSpread(spread);
			node.setCheckArr(checkArr);
			treeNodes.add(node);
		}
		return new DataGridView(treeNodes);
	}
	
	/**
     * 	保存分配角色菜单
     * @param rid 当前角色id
     * @param ids 权限id( "pid1,pid2..." )
     * @return
     */
    @RequestMapping("/saveRoleMenu")
    public JsonResult saveRoleMenu(RoleVo roleVo){
        try {
            if(roleService.saveRoleMenu(roleVo) > 0){
                return SysConstant.DISTRIBUTE_SUCCESS;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return SysConstant.DISTRIBUTE_ERROR;
    }
    
}
